package com.user.filter;

import com.user.comment.KeyStr;
import com.user.config.JwtProperties;
import com.user.entity.Payload;
import com.user.entity.UserInfo;
import com.user.util.JedisUtil;
import com.user.util.JsonUtils;
import com.user.util.JwtUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import redis.clients.jedis.Jedis;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class JwtVerifyFilter extends BasicAuthenticationFilter {
    private JwtProperties prop;
    public JwtVerifyFilter(AuthenticationManager authenticationManager, JwtProperties prop) {
        super(authenticationManager);
        this.prop=prop;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        String authorization = request.getHeader("Authorization");
        if(authorization == null){
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.setContentType("application/json;charset=utf-8");
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("msg","请先登录");
            response.getWriter().print(JsonUtils.toString(hashMap));
            return;
        }
        Payload<UserInfo> infoFromToken = JwtUtils.getInfoFromToken(authorization, prop.getPublicKey(), UserInfo.class);
        String id = infoFromToken.getId();
        Jedis jedis = JedisUtil.getJedis();
        String token = jedis.get(KeyStr.OUT_TOKET + id);
        jedis.close();
        if(!StringUtils.isBlank(token)){
            System.out.println(token);
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.setContentType("application/json;charset=utf-8");
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("msg","token已过期");
            PrintWriter writer = response.getWriter();
            writer.print(JsonUtils.toString(hashMap));
            writer.flush();
            writer.close();
            return;
        }
        UserInfo info = infoFromToken.getInfo();
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(info.getId(),info.getUsername(), info.getGrantedAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        chain.doFilter(request,response);
    }
}
